<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/31
 * Time: 13:10
 */

namespace Home\Controller;


use Org\Util\ZipFile;
use Think\Controller;

class OrdersController extends Controller
{
    //@todo 加载订单列表页
    public function index(){
        $this->display();
    }

    //@todo 获取所有订单列表
    public function getOrdersList(){
        $postData=$_GET;
        $where=array();
        if(!empty($postData['orders_no'])){
            $where['orders_no']=$postData['orders_no'];
        }
        if($postData['status'] != ''){
            $where['status']=$postData['status'];
        }else{
            $where['status']=array('NOT IN',array('6','7'));
        }

        $count=D('orders_list')->where($where)->count();
        $list=D('orders_list')->field("*,FROM_UNIXTIME(ac_time) as ac_time")->limit($_GET['offset'],$_GET['limit'])->where($where)->select();
        foreach($list as $key => $value){
           $user_info=D('client_user_address')->where(array('user_id'=>$value['user_id'],'is_code'=>'1'))->find();
            $list[$key]['user_info']=$user_info['name'];
        }
        $this->ajaxReturn(array('rows'=>$list,'total'=>$count));
    }

    //@todo 查看详情
    public function detailOrder(){

        if(!isset($_GET['id'])){
            $this->error('非法访问');
        }
        $id=$_GET['id'];
        $list=D('orders_list')->field("*,FROM_UNIXTIME(ac_time) as ac_time")->where(array('id'=>$id))->find();
        $user_info=D('client_user_address')->where(array('user_id'=>$list['user_id'],'is_code'=>'1'))->find();
        $list['user_name']=$user_info['name'];
        $list['user_phone']=$user_info['phone'];
        $list['user_address']=$user_info['address'];
        $list['order_xk']='未选择';
        //查询相框尺寸
        if($list['xk_id']!=''&&$list['xk_id']!=null){
            $xk_info=D('photo_box')->find($list['xk_id']);
            $list['order_xk']=$xk_info['text'];
        }
        //查询相册尺寸
        $list['order_xc']='未选择';
        if($list['xc_id']!=''&&$list['xc_id']!=null){
            $xc_info=D('photo_box')->find($list['xc_id']);
            $list['order_xc']=$xc_info['text'];
        }
        $this->assign('info',$list);
        $this->display();
    }

    //@todo 查看订单详细
    public function getOrderDetail(){
        $count=D('orders_goods_list')->where(array('orders_id'=>$_GET['id']))->count();

        $list=D('orders_goods_list')->where(array('orders_id'=>$_GET['id']))->limit($_GET['offset'],$_GET['limit'])->select();

        foreach($list as $key=>$value){
            $list[$key]['photo_url']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/UserPhoto/'.$value['photo_url'];
            $product=D('product_pic')->find($value['product_id']);
            if($product){
                $list[$key]['product_pic']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$product['thumb'];
                $list[$key]['product_src']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$product['pic'];
            }
            if($value['decorate_id'] == '' ||$value['decorate_id']==null){
                $list[$key]['decorate_pic']=array();
            }else{
                //查询装饰
                $decorate=D('product_next_pic')->where(array('id'=>array('in',explode(',',$value['decorate_id']))))->select();
                if($decorate){
                    foreach($decorate as $k =>$v){
                        $decorate[$k]['decorate_pic']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$v['thumb'];
                        $decorate[$k]['decorate_src']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$v['pic'];
                    }
                    $list[$key]['decorate_pic']=$decorate;
                }
            }

        }
        $this->ajaxReturn(array('rows'=>$list,'total'=>$count));
    }

    //@todo 发货
    public function giveOrdersNumber(){
        $id=$_POST['id'];
        $number_no=$_POST['number_no'];
        $save=D('orders_list')->save(array('id'=>$id,'status'=>'2','number_no'=>$number_no));
        if(!is_numeric($save)){
            $this->ajaxReturn(array('success'=>false,'message'=>'服务器发生错误，请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'操作成功'));
    }

    //@todo 删除订单
    public function del_orders(){
        if(isset($_POST['id'])){
            $id=$_POST['id'];
            $delOrdersList=D('orders_goods_list')->where(array('orders_id'=>$id))->delete();
            if(!$delOrdersList){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
            }
            $del=D('orders_list')->delete($id);
            if(!$del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }
        $this->ajaxReturn(array('success'=>false,'message'=>'异常访问'));
    }

}